﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using System.Data.SqlClient;

namespace ThuVien
{
    public partial class frmDocGia : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public frmDocGia()
        {
            InitializeComponent();
        }
        DocGiaBUS DG_BUS = new DocGiaBUS();
        DocGiaDTO DG_DTO = new DocGiaDTO();
        private void frmDocGia_Load(object sender, EventArgs e)
        {
            DG_BUS.khoitao_KN();
            txtMaDocGia.Text = DG_BUS.Phatsinh_MaDG();
            DataSet ds1 = Load_LoaiDG();
            cbxLoaiDG.DataSource = ds1.Tables[0];
            cbxLoaiDG.ValueMember = "MaLoaiDocGia";
            cbxLoaiDG.DisplayMember = "MaLoaiDocGia";
            DataSet ds2 = Load_MaNV();
            cbxNhanVienLapThe.DataSource = ds2.Tables[0];
            cbxNhanVienLapThe.ValueMember = "MaNV";
            cbxNhanVienLapThe.DisplayMember = "MaNV";
            DataSet ds = new DataSet();
            ds = DG_BUS.Hien_DocGia();
            dataGridViewX1.DataSource = ds.Tables[0];
            dataGridViewX1.Visible = true;

        }

        private void labelX2_Click(object sender, EventArgs e)
        {

        }

        private void labelX7_Click(object sender, EventArgs e)
        {

        }
        public DataSet Load_LoaiDG()
        {
            SqlConnection con = DG_BUS.ChuoiKN_Load();
            con.Open();
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string sql = "select * from loaidocgia";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet dataset = new DataSet();
            da.Fill(dataset);
            return dataset;
        }
        public DataSet Load_MaNV()
        {
            SqlConnection con = DG_BUS.ChuoiKN_Load();
            con.Open();
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string sql = "select * from nhanvien";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet dataset = new DataSet();
            da.Fill(dataset);
            return dataset;
        }

        private void btnLapThe_Click(object sender, EventArgs e)
        {

            DG_DTO.Madocgia = txtMaDocGia.Text;
            DG_DTO.Hoten = txtTenDocGia.Text;
            DG_DTO.Ngaysinh = ngaysinh.Value;
            DG_DTO.Diachi = txtDiaChi.Text;
            DG_DTO.Dienthoai = txtDienThoai.Text;
            DG_DTO.Email = txtEmail.Text;
            DG_DTO.Ngaylapthe = NgayLapThe.Value;
            DG_DTO.Ngayhethan = ngayhethan.Value;
            DG_DTO.Manguoilap = cbxNhanVienLapThe.SelectedValue.ToString();
            DG_DTO.Maloaidocgia = cbxLoaiDG.SelectedValue.ToString();
            DG_DTO.Tienno = 13;           
            DG_BUS.LapTheDocGia(DG_DTO);

            frmDocGia_Load(sender, e);



        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DG_DTO.Madocgia = txtMaDocGia.Text;
            DG_DTO.Hoten = txtTenDocGia.Text;
            DG_DTO.Ngaysinh = ngaysinh.Value;
            DG_DTO.Diachi = txtDiaChi.Text;
            DG_DTO.Dienthoai = txtDienThoai.Text;
            DG_DTO.Email = txtEmail.Text;
            DG_DTO.Ngaylapthe = NgayLapThe.Value;
            DG_DTO.Ngayhethan = ngayhethan.Value;
            DG_DTO.Manguoilap = cbxNhanVienLapThe.SelectedValue.ToString();
            DG_DTO.Maloaidocgia = cbxLoaiDG.SelectedValue.ToString();
            DG_DTO.Tienno = 13;
            DG_BUS.xoa_DocGia(DG_DTO);
            frmDocGia_Load(sender, e);

        }
        int dong;
        //private void dataGridViewX1_CellEnter(object sender, DataGridViewCellEventArgs e)
        //{


        //    try
        //    {
        //        //dataGridViewX1.Visible = true ;
        //        dong = e.RowIndex;
        //        txtMaDocGia.Text = dataGridViewX1.Rows[dong].Cells[0].Value.ToString();
        //        txtTenDocGia.Text = dataGridViewX1.Rows[dong].Cells[1].Value.ToString();
        //        ngaysinh.Value = (DateTime)dataGridViewX1.Rows[dong].Cells[2].Value;
        //        txtDiaChi.Text = dataGridViewX1.Rows[dong].Cells[3].Value.ToString();
        //        txtEmail.Text = dataGridViewX1.Rows[dong].Cells[4].Value.ToString();
        //        txtDienThoai.Text = dataGridViewX1.Rows[dong].Cells[5].Value.ToString();
        //        cbxLoaiDG.Text = dataGridViewX1.Rows[dong].Cells[6].Value.ToString();
        //        txtTienNo.Text = dataGridViewX1.Rows[dong].Cells[7].Value.ToString();
        //        cbxNhanVienLapThe.Text = dataGridViewX1.Rows[dong].Cells[8].Value.ToString();
        //    }
        //    catch { txtMaDocGia.Text = DG_BUS.Phatsinh_MaDG(); };

        //}
        ////int dong;
        private void dataGridViewX1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            //dataGridViewX1.Visible = true ;

            dong = e.RowIndex;
            try
            {
                txtMaDocGia.Text = dataGridViewX1.Rows[dong].Cells[0].Value.ToString();
                txtTenDocGia.Text = dataGridViewX1.Rows[dong].Cells[1].Value.ToString();
                ngaysinh.Value = (DateTime)dataGridViewX1.Rows[dong].Cells[2].Value;
                txtDiaChi.Text = dataGridViewX1.Rows[dong].Cells[3].Value.ToString();
                txtEmail.Text = dataGridViewX1.Rows[dong].Cells[4].Value.ToString();
                txtDienThoai.Text = dataGridViewX1.Rows[dong].Cells[5].Value.ToString();
                cbxLoaiDG.Text = dataGridViewX1.Rows[dong].Cells[6].Value.ToString();
                txtTienNo.Text = dataGridViewX1.Rows[dong].Cells[7].Value.ToString();
                cbxNhanVienLapThe.Text = dataGridViewX1.Rows[dong].Cells[8].Value.ToString();
            }
            catch
            {
                txtMaDocGia.Text = DG_BUS.Phatsinh_MaDG();
                txtMaDocGia .Text = DG_BUS.Phatsinh_MaDG();
                txtTenDocGia .Text = "";
                ngaysinh.Value = DateTime.Today;
                txtDiaChi .Text = "";
                txtDienThoai.Text = "";
                txtEmail.Text = "";
                
                //cbxChucVu.Text = "";
                //cbxBoPhan.Text = "";

            }


        }

        private void btnMoi_Click(object sender, EventArgs e)
        {
            txtMaDocGia.Text = DG_BUS.Phatsinh_MaDG();
            txtMaDocGia.Text = DG_BUS.Phatsinh_MaDG();
            txtTenDocGia.Text = "";
            ngaysinh.Value = DateTime.Today;
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtEmail.Text = "";

        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            DG_DTO.Madocgia = txtMaDocGia.Text;
            DG_DTO.Hoten = txtTenDocGia.Text;
            DG_DTO.Ngaysinh = ngaysinh.Value;
            DG_DTO.Diachi = txtDiaChi.Text;
            DG_DTO.Dienthoai = txtDienThoai.Text;
            DG_DTO.Email = txtEmail.Text;
            DG_DTO.Ngaylapthe = NgayLapThe.Value;
            DG_DTO.Ngayhethan = ngayhethan.Value;
            DG_DTO.Manguoilap = cbxNhanVienLapThe.SelectedValue.ToString();
            DG_DTO.Maloaidocgia = cbxLoaiDG.SelectedValue.ToString();
            DG_DTO.Tienno = 13;
            DG_BUS.Sus_ThongTinDocGia (DG_DTO);
            frmDocGia_Load(sender, e);


        }
    }
}

